# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def select_image_items(images, selected)
    images.collect do |image|
      content_tag("div", 
        check_box_tag("record[images][#{image.id}][id]", image.id, selected)  + 
          content_tag("span", image_preview(image, false), :class => "select_preview"),
        :class => :select_item)
    end.join("\n")
  end
  
  def select_template_item(template, selected)
    content_tag("div", 
      radio_button_tag("record[template][id]", template.id, selected) +
        content_tag("span", template_preview(template, false), :class => "select_preview"), 
      :class => :select_item)
  end

  def related_templates_column(record)
    templates_list(record.related_templates)
  end

  def related_images_column(record)
    images_list(record.related_images)
  end

  def image_preview(image, link=true)
    url = url_for_file_column(image, :filename, "thumb")
    html = image_tag(url, :title => image.title, :class => :preview) 
    html << link_to(image_tag('active_scaffold/default/magnifier', :title => '放大'), url, :class => :zoomin) if link
    html
  end

  def template_preview(template, link=true)
    preview_url = url_for_file_column(template, :preview, "thumb")
    file_url = url_for_file_column(template, :filename)
    html = image_tag(preview_url, :title => template.title, :class => :preview) 
    html << link_to(image_tag('active_scaffold/default/download', :title => '下载'), file_url, :class => :zoomin) if link
    html
  end

  def hymns_list(hymns)
    hymns.collect do |hymn|
      link_to hymn.title, :controller => :hymns, :action => :show, :id => hymn.id
    end.join(" ")
  end

  def images_list(images)
    images.collect do |image|
      link_to image_preview(image), :controller => :images, :action => :show, :id => image.id
    end.join(" ")
  end

  def templates_list(templates)
    templates.collect do |template|
      link_to template_preview(template), :controller => :templates, :action => :show, :id => template.id
    end.join(" ")
  end
  
  def tag_list_column(record)
    record.tags.collect do |t|
      link_to t.name, :controller => :tags, :action => :show, :id => t.id
    end.join(" ")
  end
end
